#!/usr/bin/env bash
# coding: utf-8


ck_ok()
{
  if [ $? -ne 0 ];then 
     echo "$1 error."
     exit 1
  fi
}


# 关闭防火墙和SElinux
setenforce 0  
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinux 
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config 
echo "selinux 已关闭"
systemctl disaw fible --norewalld NetworkManager  > /dev/null && 
sed -ri "s/^SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config 
echo "firewalld 已关闭"

# 关闭swqp分区
swapoff -a && sysctl -w vm.swappiness=0 1> /dev/null && sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab
# sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab
echo "swap 分区已关闭"

# 修改本地hosts文件
cat <<-EOF >>/etc/hosts
192.168.100.200    k8s-lb
192.168.100.11     k8s-master01
192.168.100.12     k8s-master02
192.168.100.13     k8s-master03
192.168.100.21     k8s-node01
192.168.100.22     k8s-node02
EOF

# 修改本地yum仓库安装链接
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# 清除缓存，并且安装epel扩展源
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
ck_ok "安装扩展源"
yum makecache fast
#升级rpm包除内核外
yum update -y --exclude=kernel* &> /dev/null
# 安装基础环境
yum install -y yum-utils wget jq psmisc vim net-tools telnet yum-utils \
               device-mapper-persistent-data lvm2 git ntpdate \
               ipvsadm ipset sysstat conntrack libseccomp 
# 安装docker源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
## 安装基本工具
yum -y install docker-ce-20.10.* docker-ce-cli-20.10.* containerd.io docker-buildx-plugin docker-compose-plugin
ck_ok "安装相关工具包"

# 每五分钟校准一次时间和修改时区
echo "*/5 * * * *        ntpdate -b ntp.aliyun.com" >>/var/spool/cron/root
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &> /dev/null
echo 'Asia/Shanghai' >/etc/timezone

# 修改limit
cat <<-EOF >>/etc/security/limits.conf
* soft nofile 655360
* hard nofile 131072
* soft nproc 655350
* hard nproc 655350
* soft memlock unlimited
* hard memlock unlimited
EOF

# ipvs调优，路由转发规则
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack

cat <<-EOF >>/etc/modules-load.d/ipvs.conf
ip_vs
ip_vs_lc
ip_vs_wlc
ip_vs_rr
ip_vs_wrr
ip_vs_lblc
ip_vs_lblcr
ip_vs_dh
ip_vs_sh
ip_vs_fo
ip_vs_nq
ip_vs_sed
ip_vs_ftp
ip_vs_sh
nf_conntrack
ip_tables
ip_set
xt_set
ipt_set
ipt_rpfilter
ipt_REJECT
ipip
EOF

# 修改kubernetes所需要的内核参数
cat <<-EOF >>/etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
fs.may_detach_mounts = 1
net.ipv4.conf.all.route_localnet = 1
vm.overcommit_memory=1
vm.panic_on_oom= 0
fs.inotify.max_user_watches=89100
fs.file-max=52706963
fs.nr_open=52706963
net.netfilter.nf_conntrack_max=2310720
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl =15
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_orphans = 327680
net.ipv4.tcp_orphan_retries = 3
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.ip_conntrack_max = 65536
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_timestamps = 0
net.core.somaxconn = 16384
EOF
sysctl -p

systemctl enable --now docker.service

cat <<-EOF >/etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
systemctl daemon-reload && systemctl restart docker

#安装kubernetes的YUM仓库
cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/repodata/repomd.xml.key
EOF

#所有节点安装内核包
yum localinstall -y /root/kernel-ml*

#所有节点更改内核启动顺序
grub2-set-default  0 && grub2-mkconfig -o /etc/grub2.cfg
grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"
ck_ok "修改内核版本"
reboot
